package com.aso114.project.base;


import com.aso114.project.util.retrofit.ApiStores;
import com.aso114.project.util.retrofit.AppClient;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by Administrator on 2016/10/14 0014.
 */

public class BaseModel {
    private CompositeSubscription mCompositeSubscription;
    public ApiStores apiStores = AppClient.retrofit().create(ApiStores.class);
    /**
     * @param observable 被观察者
     * @param subscriber 观察者
     */
    public void addSubscription(Observable observable, Subscriber subscriber) {

        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }
        mCompositeSubscription.add(observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber));
    }
    /**
     * RXjava取消注册，以避免内存泄露
     */

    public void onUnsubscribe() {
        if (mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions()) {
            mCompositeSubscription.unsubscribe();
        }
    }
}
